#include "Game.h"
#include "Object.h"
#include "ViewFactory.h"
#include "TerrainFactory.h"
#include "StartScreen.h"
#include "GameScreen.h"
#include "EndingScreen.h"
#include "Tank.h"
#include <vector>

Game::Game()
{
	//attribute setting
	onGame = false;
	pass_missile_num = 0;
	map = nullptr;
	scr = nullptr;
	view = VIEW_TWO;

	//enable Loading Screen & Loading
	Loading();

	//enable game start screen
	scr = new StartScreen();
}
void Game::GameStart()
{
	//attribute setting
	onGame = true;
	pass_missile_num = 0;
	clearObjList();

	//camera setting
	//get default view
	ViewFactory::getDefaultView();

	//map generating
	map = TerrainFactory::get_map();

	//enable game screen
	delete scr;
	scr = new GameScreen();

	player = new Turret(0, 100, 500, 400, model_list[TURRET_BASE], model_list[TURRET_BARREL]);

	//enable enemy generating
	//addObject(new Tank(0, 20, 0, 1, model_list[TANK_PROJECT]));

	//temp = new Missile(50, 20, 200, 100, 100, 10, 10);
}

void Game::GameEnd()
{
	//clear object_list
	clearObjList();

	//attribute setting
	onGame = true;

	//enable score screen
	delete scr;
	scr = new EndingScreen();
}
void Game::changeView(VIEWS _view)
{
	//change view
	view = _view;
}
void Game::Shoot()
{
	//generate missile object
}
void Game::Loading()
{
	model_list.resize(5);
	//read all object file
	enum MODEL_TYPE {AIRCRAFT1, AIRCRAFT2, TANK_PROJECT, TURRET_BARREL, TURRET_BASE};
	model_list[AIRCRAFT1] = new Model("aircraft1.obj");
	model_list[AIRCRAFT2] = new Model("aircraft2.obj");
	model_list[TANK_PROJECT] = new Model("tank_project.obj");
	model_list[TURRET_BARREL] = new Model("turret_barrrel.obj");
	model_list[TURRET_BASE] = new Model("turret_base.obj");

	std::cout<<"Loading Complete"<<std::endl;
}
void Game::addObject(Object* obj)
{
	if(onGame)
		object_list.push_back(obj);
}
void Game::rotate(int rotate_theta, int lift)
{
	//change model matrix
	player->Rotate(rotate_theta, lift);
}
void Game::Update()
{
	pass_missile_num++;
}
void Game::DrawAllObject()
{
	int i =0;
	for(i = 0 ; i < object_list.size() ; i++)
		object_list[i]->Draw();
}
void Game::Draw()
{
	//std::cout<<object_list.size()<<std::endl;
	if(!onGame)
	{
		scr->Draw();
	}
	else
	{
		scr->Draw();
		ViewFactory::getView(view, nullptr);

		glMatrixMode(GL_MODELVIEW);
		map->Draw();
		player->Draw();
		//temp->Draw();
		DrawAllObject();
		//temp->Draw();
	}
}
void Game::clearObjList()
{
	for(int i = 0 ; i < object_list.size() ; i++)
	{
		delete object_list[i];
	}

	object_list.clear();
}